package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	
	public class EmbossAction extends AbstractAction
	{
		private var direction:int;
		private var record:String;
		public static const DIRECTION_TOP:int = 0;
		public static const DIRECTION_BOTTOM:int = 1;
		public static const DIRECTION_LEFT:int = 2;
		public static const DIRECTION_RIGHT:int = 3;
		public static const DIRECTION_TOP_LEFT:int = 4;
		public static const DIRECTION_TOP_RIGHT:int = 5;
		public static const DIRECTION_BOTTOM_LEFT:int = 6;
		public static const DIRECTION_BOTTOM_RIGHT:int = 7;
		
		public function setValue(direction:int):EmbossAction
		{
			this.direction = direction;
			switch(direction)
			{
				case DIRECTION_BOTTOM:
                {
                    record = "Bottom"
                    break;
                }
                case DIRECTION_BOTTOM_LEFT:
                {
                    record = "BottomLeft"
                    break;
                }
                case DIRECTION_BOTTOM_RIGHT:
                {
                    record = "BottomRight"
                    break;
                }
                case DIRECTION_LEFT:
                {
                    record = "Left"
                    break;
                }
                case DIRECTION_RIGHT:
                {
                    record = "Right"
                    break;
                }
                case DIRECTION_TOP:
                {
                    record = "Top"
                    break;
                }
                case DIRECTION_TOP_RIGHT:
                {
                    record = "TopRight"
                    break;
                }
                case DIRECTION_TOP_LEFT:
                {
                    record = "TopLeft"
                    break;
                }
                default:
                {
                	throw new Error("Invalid emboss direction"); 
                }
			}
			
			return this;
		}
		
		public override function getDescription():String
		{
			return "Emboss (" + this.record + ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var ret:BitmapData = old.clone();
			var m:Array;
			switch(direction){
				case DIRECTION_BOTTOM:
                {
                    m = [0, -2, 0, 0, 0, 0, 0, 2, 0];
                    break;
                }
                case DIRECTION_BOTTOM_LEFT:
                {
                    m = [0, 0, -2, 0, 0, 0, 2, 0, 0];
                    break;
                }
                case DIRECTION_BOTTOM_RIGHT:
                {
                    m = [-2, 0, 0, 0, 0, 0, 0, 0, 2];
                    break;
                }
                case DIRECTION_LEFT:
                {
                    m = [0, 0, 0, 2, 0, -2, 0, 0, 0];
                    break;
                }
                case DIRECTION_RIGHT:
                {
                    m = [0, 0, 0, -2, 0, 2, 0, 0, 0];
                    break;
                }
                case DIRECTION_TOP:
                {
                    m = [0, 2, 0, 0, 0, 0, 0, -2, 0];
                    break;
                }
                case DIRECTION_TOP_RIGHT:
                {
                    m = [0, 0, 2, 0, 0, 0, -2, 0, 0];
                    break;
                }
                case DIRECTION_TOP_LEFT:
                {
                    m = [2, 0, 0, 0, 0, 0, 0, 0, -2];
                    break;
                }
			}
			var convolutionFilter:ConvolutionFilter  = new ConvolutionFilter(3, 3, m, 1, 128);
			var greyScaleFilter:ColorMatrixFilter = new ColorMatrixFilter();
			greyScaleFilter.matrix = [0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 
										0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0];
			
			ret.applyFilter(ret, ret.rect, new Point(0, 0), greyScaleFilter);
			ret.applyFilter(ret, ret.rect, new Point(0, 0), convolutionFilter);
			return ret;
		}	
	}
}